import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { LikeVideo } from './likeVideo.entity';

@Injectable()
export class LikeVideoService {
  constructor(
    @InjectRepository(LikeVideo)
    private likeVideoRepository: Repository<LikeVideo>,
  ) {}

  findAll(): Promise<LikeVideo[]> {
    return this.likeVideoRepository.find();
  }

  findOne(id: number): Promise<LikeVideo> {
    return this.likeVideoRepository.findOne({where:{id}});
  }

  findOne_by_video_id(video_id: number, wx_user?: string): Promise<LikeVideo> {
    return this.likeVideoRepository.findOne({where:{video_id,wx_user}});
  }

  async remove(id: number): Promise<void> {
    await this.likeVideoRepository.delete(id);
  }

  async remove_by_video_id(video_id: number, wx_user: string): Promise<void> {
    let findWxLike = await this.likeVideoRepository.findOne({where:{video_id: video_id, wx_user: wx_user}})
    console.log(findWxLike,'ff')
    if(findWxLike && findWxLike.share_wx_user){
      await this.likeVideoRepository.update({id:findWxLike.id}, {delete_tag: 1})
    }else{
      await this.likeVideoRepository.delete({video_id, wx_user});
    }
  }

  async create(likeVideo: LikeVideo): Promise<void> {
    let findVideo = await this.likeVideoRepository.query(`
      select video.*,
      (
        select v.id from video as v
        inner join platform on platform.ic_group = v.ic_group
        inner join internet_celebrity_group on internet_celebrity_group.id = v.ic_group
        where v.id = video.id and v.check = 1 and v.source = 2 and v.ic_group is not null
          and v.id not in (
            select video.id from video
            where video.check = 1 and video.source = 2 and (
              video.ic_group in (
                select a.id from (
                  select distinct internet_celebrity_group.id from internet_celebrity_group 
                  left join platform on platform.ic_group = internet_celebrity_group.id
                  inner join video on video.ic_group = internet_celebrity_group.id
                  where (
                    (
                      internet_celebrity_group.type = 2 
                    )
                    or(
                      internet_celebrity_group.type = 0 and platform.platform in (select setting.value from setting where setting.key = 'level3')
                    )
                    or (
                      internet_celebrity_group.type = 3 
                    )
                  )
                ) a
              )
              or ((video.overseas = 3 or video.overseas = 1) and video.original = 1)
            )
          )
        limit 0,1
      ) as is_lib
      from video
      where video.id = ?
    `,[likeVideo.video_id])
    //查询是否有过收藏
    if(findVideo.length > 0 && findVideo[0].is_lib){
      //查询当前视频自己是否没有同提问，并且别人同提问且收到答案了
      let findQuestion = await this.likeVideoRepository.query(`
        select questions.id from questions where video_id = ? and wx_user = ?
      `, [likeVideo.video_id, likeVideo.wx_user])
      let findAnswer = await this.likeVideoRepository.query(`
        select questions.wx_user from questions where video_id = ? and rela_questions_wx_user is null and post_answer = 1
      `, [likeVideo.video_id])
      console.log(findQuestion, 'findAnswer', findAnswer)

      if(findQuestion.length === 0 && findAnswer.length > 0){
        likeVideo.share_wx_user = findAnswer[0].wx_user 
      }
    }
    let findWxLike = await this.likeVideoRepository.findOne({where:{video_id: likeVideo.video_id, wx_user: likeVideo.wx_user, delete_tag: 1}})
    if(findWxLike){
      likeVideo.delete_tag = 0
      likeVideo.id = findWxLike.id
    }
    await this.likeVideoRepository.save(likeVideo);
  }

  async update(likeVideo: LikeVideo): Promise<void> {
    await this.likeVideoRepository.save(likeVideo);
  }
}
